import { apiClient } from './api-client';
import { 
  ConfigInfo, 
  ConfigQueryParams, 
  ConfigRequest, 
  BaseResponse,
  PaginationResponse 
} from '../types/api.types';

export class ConfigService {
  private static readonly BASE_URL = '/config';

  /**
   * 获取配置列表（分页）
   * @param params 查询参数
   * @returns 配置列表
   */
  static async getConfigList(params: ConfigQueryParams): Promise<BaseResponse<PaginationResponse<ConfigInfo>>> {
    try {
      const response = await apiClient.get<PaginationResponse<ConfigInfo>>(
        `${this.BASE_URL}/list`,
        params
      );
      return response;
    } catch (error) {
      console.error('获取配置列表失败:', error);
      throw error;
    }
  }

  /**
   * 获取配置详情
   * @param configId 配置ID
   * @returns 配置信息
   */
  static async getConfigDetail(configId: number): Promise<BaseResponse<ConfigInfo>> {
    try {
      // 这里假设有获取配置详情的接口
      const response = await apiClient.get<ConfigInfo>(
        `${this.BASE_URL}/${configId}`
      );
      return response;
    } catch (error) {
      console.error('获取配置详情失败:', error);
      throw error;
    }
  }

  /**
   * 创建配置
   * @param configData 配置数据
   * @returns 创建结果
   */
  static async createConfig(configData: ConfigRequest): Promise<BaseResponse<string>> {
    try {
      const response = await apiClient.post<string>(
        this.BASE_URL,
        configData
      );
      return response;
    } catch (error) {
      console.error('创建配置失败:', error);
      throw error;
    }
  }

  /**
   * 更新配置信息
   * @param configData 配置数据
   * @returns 更新结果
   */
  static async updateConfig(configData: ConfigRequest): Promise<BaseResponse<string>> {
    try {
      const response = await apiClient.put<string>(
        this.BASE_URL,
        configData
      );
      return response;
    } catch (error) {
      console.error('更新配置失败:', error);
      throw error;
    }
  }

  /**
   * 删除配置
   * @param configId 配置ID
   * @returns 删除结果
   */
  static async deleteConfig(configId: number): Promise<BaseResponse<string>> {
    try {
      const response = await apiClient.delete<string>(
        `${this.BASE_URL}/${configId}`
      );
      return response;
    } catch (error) {
      console.error('删除配置失败:', error);
      throw error;
    }
  }

  /**
   * 批量删除配置
   * @param configIds 配置ID数组
   * @returns 批量删除结果
   */
  static async batchDeleteConfigs(configIds: number[]): Promise<BaseResponse<string>> {
    try {
      const response = await apiClient.delete<string>(
        `${this.BASE_URL}/batch`,
        { body: configIds }
      );
      return response;
    } catch (error) {
      console.error('批量删除配置失败:', error);
      throw error;
    }
  }

  /**
   * 搜索配置
   * @param keyword 关键词
   * @returns 搜索结果
   */
  static async searchConfigs(keyword: string): Promise<BaseResponse<ConfigInfo[]>> {
    try {
      // 这里假设有搜索接口，或者使用列表接口进行搜索
      const response = await apiClient.get<ConfigInfo[]>(
        `${this.BASE_URL}/search`,
        { keyword }
      );
      return response;
    } catch (error) {
      console.error('搜索配置失败:', error);
      throw error;
    }
  }

  /**
   * 获取所有配置（不分页）
   * @returns 所有配置列表
   */
  static async getAllConfigs(): Promise<BaseResponse<ConfigInfo[]>> {
    try {
      const response = await apiClient.get<ConfigInfo[]>(
        `${this.BASE_URL}/all`
      );
      return response;
    } catch (error) {
      console.error('获取所有配置失败:', error);
      throw error;
    }
  }

  /**
   * 根据配置名称获取配置值
   * @param name 配置名称
   * @returns 配置值
   */
  static async getConfigByName(name: string): Promise<BaseResponse<ConfigInfo>> {
    try {
      const response = await apiClient.get<ConfigInfo>(
        `${this.BASE_URL}/name/${name}`
      );
      return response;
    } catch (error) {
      console.error('根据名称获取配置失败:', error);
      throw error;
    }
  }
}

export default ConfigService;